﻿using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class VIPCarousel : MonoBehaviour {

    private VIPButton slideBut;
    private RectTransform slideRectTransform;

    /// <summary>
    /// 记录按下的点
    /// </summary>
    private Vector3 recordPoint;

    /// <summary>
    /// 内容父对象
    /// </summary>
    [SerializeField]
    private Transform content;

    /// <summary>
    /// 存放新闻的集合
    /// </summary>
    private List<Transform> slides = new List<Transform>();

    /// <summary>
    /// 集合的长度
    /// </summary>
    private int slideLenght;

    /// <summary>
    /// 当前下标
    /// </summary>
    private int count = 0;

    /// <summary>
    /// 动画间隔速度
    /// </summary>
    [SerializeField]
    private float intervalSpeed = 3;

    /// <summary>
    /// 累加时间
    /// </summary>
    private float countTime = 0;

    /// <summary>
    /// 滚动速度
    /// </summary>
    [SerializeField]
    private float rollSpeed = 0.3f;

    /// <summary>
    /// 是否自动轮播
    /// </summary>
    [SerializeField]
    private bool isAuto;

    /// <summary>
    /// 滑动面板的宽度
    /// </summary>
    private float wight = 0;

    private void Awake()
    {
        slideBut = GetComponent<VIPButton>();
        slideRectTransform = GetComponent<RectTransform>();
        //初始宽度
        wight = slideRectTransform.sizeDelta.x;
    }

    private void Start()
    {
        //按下
        slideBut.onDown.AddListener(() =>
        {
            SlideOnDown();
        });
        //抬起
        slideBut.onUp.AddListener(() =>
        {
            SlideOnUp();
        });
        slideLenght = content.childCount;
        foreach (Transform slide in content)
        {
            slides.Add(slide);
        }
    }

    private void Update()
    {
        if (isAuto) {
            if (countTime > intervalSpeed) {
                countTime = 0;
                //自动轮播
                if (count > -slideLenght + 1)
                {
                    LeftMove();
                }
                else
                {
                    count = 0;
                    //初始宽度
                    wight = slideRectTransform.sizeDelta.x;
                    content.DOLocalMoveX(wight * count, rollSpeed);
                }
            }
            countTime += Time.deltaTime;
        }
    }

    /// <summary>
    /// 滑动按钮按下
    /// </summary>
    private void SlideOnDown()
    {
        //记录按下的点
        recordPoint = Input.mousePosition;
    }


    /// <summary>
    /// 滑动按钮抬起
    /// </summary>
    private void SlideOnUp()
    {
        //记录当前的点
        Vector3 currentPoint = Input.mousePosition;
        //计算滑动方向
        Vector2 v2 = VIPComputeUtil.ComputeSlipDirection(recordPoint, currentPoint);
        if (v2.x > 0) {
            //左滑到末尾
            LeftMove();
        }
        if (v2.x < 0) {
            //右滑到起始位置即可
            RightMove();
        }
    }

    /// <summary>
    /// 左滑
    /// </summary>
    public void LeftMove() {
        if (count > -slideLenght + 1)
        {
            count--;
            content.DOLocalMoveX(wight * count, rollSpeed);
        }
    }

    /// <summary>
    /// 右滑
    /// </summary>
    public void RightMove() {
        if (count < 0)
        {
            count++;
            content.DOLocalMoveX(wight * count, rollSpeed);
        }
    }

}
